package com.yfp.client.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.yfp.client.domain.MarCoupon;
import com.yfp.client.domain.MarCouponPack;
import com.yfp.client.domain.MarCouponPackDetails;
import com.yfp.client.mapper.MarCouponPackDetailsMapper;
import com.yfp.client.mapper.MarCouponPackMapper;
import com.yfp.client.service.MarCouponPackService;
import com.yfp.client.service.MarCouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 礼包
 *
 * @CreateDate: 2024/9/27 16:24
 * @UpdateUser:
 * @UpdateDate: 2024/9/27 16:24
 * @UpdateRemark: 暂无修改
 * @Version: 1.0
 */
@Service
@Slf4j
public class MarCouponPackServiceImpl implements MarCouponPackService {
    @Autowired
    private MarCouponPackMapper marCouponPackMapper;
    @Autowired
    private MarCouponPackDetailsMapper marCouponPackDetailsMapper;
    @Autowired
    private MarCouponService marCouponService;

    @Override
    public MarCouponPack findById(Long id) {
        return marCouponPackMapper.findById(id);
    }

    @Override
    public MarCouponPack findByMarketingId(Long marketingId) {
        MarCouponPack marCouponPack = marCouponPackMapper.findByMarketingId(marketingId);
        List<MarCouponPackDetails> marCouponPackDetails = marCouponPackDetailsMapper.findByCouponPackId(marCouponPack.getId());
        log.info("marCouponPackDetails：{}", marCouponPackDetails);
        List<Long> marCouponIds = marCouponPackDetails.stream().map(s -> s.getCouponId()).collect(Collectors.toList());
        log.info("marCouponIds：{}", marCouponIds);
        List<MarCoupon> marCoupons = marCouponService.findByIds(marCouponIds);
        for (MarCoupon marCoupon : marCoupons) {
            for (MarCouponPackDetails marCouponPackDetail : marCouponPackDetails) {
                if (marCoupon.getId().equals(marCouponPackDetail.getCouponId())) {
                    log.info("marCouponPackName:{}, CouponCount", marCouponPack.getPackName(), marCouponPackDetail.getCouponCount());
                    marCoupon.setCouponCount(marCouponPackDetail.getCouponCount());
                    break;
                }
            }
        }
        marCouponPack.setMarCouponPackDetails(marCouponPackDetails);
        marCouponPack.setMarCouponList(marCoupons);
        return marCouponPack;
    }

    @Override
    public void editSurplusCount(Long id) {
        marCouponPackMapper.editSurplusCount(id);
    }

    @Override
    public List<MarCouponPack> listByIds(List<Long> couponPackIdlist) {
        if (CollUtil.isNotEmpty(couponPackIdlist)) {
            return marCouponPackMapper.listByIds(couponPackIdlist);
        }
        return List.of();
    }

    @Override
    public Map<Long, List<MarCoupon>> listCouponByCouponPackIds(List<Long> couponPackIdlist) {
        Map<Long, List<MarCoupon>> marCouponMayByCouponPackIds = couponPackIdlist.stream().collect(Collectors.toMap(couponPackId -> couponPackId, couponPackId -> marCouponService.listByCouponPackId(couponPackId), (existing, replacement) -> existing));
        return marCouponMayByCouponPackIds;
    }

    @Override
    public void editSurplusCountById(Long id, Integer surplusCount) {
        marCouponPackMapper.editSurplusCountById(id, surplusCount);
    }
}
